﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Fish_Atlantis.ProfessorPanel
{
    public partial class ProfessorEvoluate : System.Web.UI.Page
    {
        string connStr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        SqlConnection conn;
        static int riskrank = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(connStr);
            //conn.Open();//何时关闭，如果放入到ListBox1_SelectedIndexChanged中是否连接过于频繁
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            conn.Open();
            string sql = "select Category from Fishery where Location='" + ListBox1.SelectedValue.ToString() + "';";
            ListBox2.Items.Clear();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();

                da.Fill(ds);
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListBox2.Items.Add(dt.Rows[i]["Category"].ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
            conn.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedValue.ToString() == "" || ListBox2.SelectedValue.ToString() == "" || RadioButtonList1.SelectedValue.ToString() == "")
                Response.Write(Helper.MessageBox("信息填写不完全", HttpContext.Current.Request.Url.PathAndQuery));
            //Response.Write("<script language=javascript>alert("+"信息填写不完全"+");</script>") ;
            else
                Helper.ExecSQL("UPDATE Risk SET Risk_Rank='" + riskrank + "'WHERE Risk.FisheryID =(SELECT FisheryID FROM Fishery WHERE Location = '" + ListBox1.SelectedValue.ToString() + "'AND Category ='" + ListBox2.SelectedValue.ToString() + "');");
            Helper.ExecSQL("UPDATE Risk SET Comment='" + TextBox1.Text.ToString() + "'WHERE Risk.FisheryID =(SELECT FisheryID FROM Fishery WHERE Location = '" + ListBox1.SelectedValue.ToString() + "'AND Category ='" + ListBox2.SelectedValue.ToString() + "');");
            Response.Write(Helper.MessageBox("保存成功", HttpContext.Current.Request.Url.PathAndQuery));

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedValue.ToString() == "高风险")
                riskrank = 3;
            else if (RadioButtonList1.SelectedValue.ToString() == "中风险")
                riskrank = 2;
            else
                riskrank = 1;
        }
    }
}